package org.jeecg.modules.biz.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.biz.entity.EventBet;
import org.jeecg.modules.biz.entity.EventSettle;
import org.jeecg.modules.biz.service.EventBetService;
import org.jeecg.modules.biz.service.EventSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/*
 认筹 前端控制器
 */

@Api(tags = "认筹")
@RestController
@RequestMapping("/bet")
public class EventBetController {
    @Autowired
    private EventBetService betService;
    @Autowired
    private EventSettleService settleService;

    @ApiOperation("认筹列表-管理员可用")
    @RequestMapping(value = "/all/list", method = RequestMethod.GET)
    public Result<IPage<EventBet>> list(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                        @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                        EventBet eventBet,
                                        HttpServletRequest req){
        Result<IPage<EventBet>> result = new Result<>();

        QueryWrapper<EventBet> queryWrapper = QueryGenerator.initQueryWrapper(eventBet, req.getParameterMap());
        Page<EventBet> page = new Page<>(pageNo, pageSize);
        IPage<EventBet> pageList = betService.page(page, queryWrapper);
        result.setResult(pageList);

        return result;
    }

    @ApiOperation("认筹列表-管理员可用")
    @RequestMapping(value = "/isSettle/list", method = RequestMethod.GET)
    public Result<IPage<EventBet>> isSettle(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            String timestampBegin,
                                            String timestampEnd){
        Result<IPage<EventBet>> result = new Result<>();

        Page<EventBet> page = new Page<>(pageNo, pageSize);
        IPage<EventBet> pageList = betService.list4IsSettle(page, timestampBegin, timestampEnd);
        result.setResult(pageList);

        return result;
    }

    @ApiOperation("认筹列表-用户可用")
    @RequestMapping(value = "/list/{address}", method = RequestMethod.GET)
    public Result<List<EventBet>> list4Address(@PathVariable String address){
        Result<List<EventBet>> result = new Result<>();

        QueryWrapper<EventBet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("caller", address);
        queryWrapper.orderByDesc("timestamp");

        List<EventBet> list = betService.list(queryWrapper);
        result.setResult(list);

        return result;
    }

    @ApiOperation("挖矿收益列表")
    @RequestMapping(value = "/mine/list/{address}", method = RequestMethod.GET)
    public Result<List<EventSettle>> mineList(@PathVariable String address){
        Result<List<EventSettle>> result = new Result<>();

        QueryWrapper<EventSettle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("caller", address);
        queryWrapper.orderByDesc("timestamp");

        List<EventSettle> list = settleService.list(queryWrapper);
        for(EventSettle settle : list){
            BigDecimal profit = new BigDecimal(settle.getProfit());
            BigDecimal currency = new BigDecimal(0.91);
            settle.setProfit(profit.multiply(currency).setScale(0,BigDecimal.ROUND_DOWN).longValue());

        }
        result.setResult(list);

        return result;
    }

    @ApiOperation("分红收益列表")
    @RequestMapping(value = "/rebate/list/{address}", method = RequestMethod.GET)
    public Result<List<EventSettle>> rebateList(@PathVariable String address){
        Result<List<EventSettle>> result = new Result<>();

        QueryWrapper<EventSettle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("inviter", address);
        queryWrapper.orderByDesc("timestamp");

        List<EventSettle> list = settleService.list(queryWrapper);
        for(EventSettle settle : list){
            BigDecimal profit = new BigDecimal(settle.getProfit());
            BigDecimal currency = new BigDecimal(0.05);
            settle.setProfit(profit.multiply(currency).setScale(0,BigDecimal.ROUND_DOWN).longValue());
        }
        result.setResult(list);

        return result;
    }

}
